/*
 * Copyright (C) 2016 Nishant Srivastava
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.github.nisrulz.sensey;

import ohos.agp.components.Component;
import ohos.multimodalinput.event.TouchEvent;

public class TouchTypeDetector {
    class  GestureListener implements Component.DoubleClickedListener, Component.LongClickedListener,
            Component.ClickedListener,Component.TouchEventListener{
        private static final int SWIPE_MIN_DISTANCE = 120;
        private static final int SWIPE_THRESHOLD_VELOCITY = 200;

        @Override
        public void onDoubleClick(Component component) {
            touchTypListener.onDoubleTap();
        }

        @Override
        public void onClick(Component component) {
            touchTypListener.onSingleTap();
        }

        @Override
        public void onLongClicked(Component component) {
            touchTypListener.onLongPress();
        }

        @Override
        public boolean onTouchEvent(Component component, TouchEvent touchEvent) {
            switch (touchEvent.getAction()) {
                case TouchEvent.OTHER_POINT_DOWN:
                case TouchEvent.PRIMARY_POINT_DOWN:
                    int pointerCount = touchEvent.getPointerCount();
                    if (pointerCount == 3) {
                        touchTypListener.onThreeFingerSingleTap();
                    } else if (pointerCount == 2) {
                        touchTypListener.onTwoFingerSingleTap();
                    }
                    return true;
            }
            return false;
        }
    }

    public interface TouchTypListener {

        void onDoubleTap();

        void onLongPress();

        void onScroll(int scrollDirection);

        void onSingleTap();

        void onSwipe(int swipeDirection);

        void onThreeFingerSingleTap();

        void onTwoFingerSingleTap();
    }

    public static final int SCROLL_DIR_UP = 1;

    public static final int SCROLL_DIR_RIGHT = 2;

    public static final int SCROLL_DIR_DOWN = 3;

    public static final int SCROLL_DIR_LEFT = 4;

    public static final int SWIPE_DIR_UP = 5;

    public static final int SWIPE_DIR_RIGHT = 6;

    public static final int SWIPE_DIR_DOWN = 7;

    public static final int SWIPE_DIR_LEFT = 8;

    final GestureListener gestureListener; // it's needed for TouchTypeDetectorTest, don't remove

    private final TouchTypListener touchTypListener;
    private Component component;
    public TouchTypeDetector(Component component, TouchTypListener touchTypListener) {
        gestureListener = new GestureListener();
        this.component = component;
        this.touchTypListener = touchTypListener;
        this.component.setLongClickedListener(gestureListener);
        this.component.setClickedListener(gestureListener);
        this.component.setDoubleClickedListener(gestureListener);
        this.component.setTouchEventListener(gestureListener);
    }

    public void unregisterListener(){
        component.setLongClickedListener(null);
        component.setClickedListener(null);
        component.setDoubleClickedListener(null);
        component.setTouchEventListener(null);
    }
}
